<?php

namespace app\admin\controller;

use app\common\BaseHelper as Helper;
use app\common\Common;
use app\admin\model\Image;

use think\Request;

class Productcategory extends Base
{

    public function _initialize()
    {
        parent::_initialize();

        $defaultWhere = $this->getDefaultWhere();
        $this->defaultWhere = $defaultWhere;
    }

    public function index(){

        return parent::index();
    }

    /**
     * Add
     */
    public function add(Request $request){

        return parent::add($request);
    }

    /**
     * Edit
     */
    public function edit(Request $request){

        return parent::edit($request);
    }

    /**
     * Save Post
     */
    public function savePost(Request $request, $redirect = ''){
        $model = model($this->model);

        if ($request->isPost()) {
            $data = $request->param();
            if ($data['id']) {
                // Update data
                if($data['status'] == 2){
                    // 该分类下是否有商品
                    $usedInfo = $this->isUsed($data['id']);
                    if($usedInfo['code'] != 1){
                        return $this->error($usedInfo['msg']);
                    }
                }

                $result = $model->allowField(true)->save($data, ['id' => $data['id']]);
                $logInfo = $this->admin->name . '更新了一条' . lang($this->model) . '数据。';
                $afterAction = '_after_update';
            } else {
                // Insert data
                $data['status'] = 1;
                $result = $model->save($data);
                $logInfo = $this->admin->name . '添加了一条' . lang($this->model) . '数据。';
                $afterAction = '_after_insert';
            }

            if($result !== false) {
                // Query执行后的操作
                $model->$afterAction($data);

                // 写入日志
                Common::adminLog($request, $logInfo);

                return $this->success('保存成功！', 'admin/' . strtolower($this->model) . '/index');
            } else {
                return $this->error($model->getError());
            }
        } else {
            return $this->error('提交方式错误！');
        }
        //return parent::editPost($request, $redirect);
    }

    /**
     * 可见/不可见
     */
    public function handle(Request $request){
        $model = model($this->model);

        if ($request->isPost()) {
            $data = $request->param();

            // 该分类下是否有商品
            $usedInfo = $this->isUsed($data['id']);
            if($usedInfo['code'] != 1){
                return $this->error($usedInfo['msg']);
            }

            $result = $model->allowField(true)->save($data, ['id' => $data['id']]);
            if($result !== false) {
                return $this->success('操作成功！', 'admin/' . strtolower($this->model) . '/index');
            } else {
                return $this->error($model->getError());
            }
        } else {
            return $this->error('提交方式错误！');
        }
    }

    /**
     * Delete
     */
    public function delete(){
        $request = $this->request;
        $id = $request->param('id');

        $model = model($this->model);
        $info = $model->find(['id', $id]);
        if(!$info){
            return redirect('admin/'.$this->model.'/index');
        }

        // 该分类下是否有商品
        $usedInfo = $this->isUsed($id);
        if($usedInfo['code'] != 1){
            return $this->error($usedInfo['msg']);
        }

        $result = $model->where('id', $id)->delete();
        if ($result !== false) {
            // Query执行后的操作
            $model->_after_delete($id);

            $logInfo = $this->admin->name . '删除了1条' . lang($this->model) . '数据。';
            Common::adminLog($request, $logInfo);

            return $this->success('删除成功！', 'admin/' . strtolower($this->model) . '/index');
        } else {
            return $this->error('删除失败！', 'admin/' . strtolower($this->model) . '/index');
        }
    }

    /**
     * 筛选条件
     */
    public function getFilterWhere($request){
        $param = $request->param();
        $where = [];
        if(isset($param['name']) && $param['name']){
            $where['name'] = ['like', '%'.$param['name'].'%'];
        }

        return $where;
    }

    /**
     * 是否被使用过
     */
    protected function isUsed($id){
        if(!$id){
            return ['code' => 0, 'msg' => '参数错误'];
        }
        // 是否有该分类的商品
        $productWhere = [
            'category_id' => $id,
            'status' => 1
        ];
        $product = \app\admin\model\Product::where($productWhere)->find();
        if($product){
            return ['code' => 0, 'msg' => '该分类下有商品，请先删除商品'];
        }

        return ['code' => 1, 'msg' => ''];
    }

}